package com.example.literj.kt.android.plugins

import com.example.literj.kt.core.Scheduler
import com.example.literj.kt.internal.util.ExceptionHelper
import java.util.*
import java.util.concurrent.Callable

class RxAndroidPlugins {
    companion object {
        @JvmStatic
        fun onMainThreadScheduler(scheduler: Scheduler?): Scheduler {
            if (scheduler == null) {
                throw NullPointerException("scheduler == null")
            }
            return scheduler
        }

        @JvmStatic
        fun initScheduler(defaultScheduler: Callable<Scheduler>): Scheduler {
            Objects.requireNonNull(defaultScheduler, "Scheduler Supplier can't be null")
            return callRequireNonNull(defaultScheduler)
        }

        fun callRequireNonNull(s: Callable<Scheduler>): Scheduler {
            return try {
                val scheduler =
                    s.call() ?: throw NullPointerException("Scheduler Callable returned null")
                scheduler
            } catch (ex: Throwable) {
                throw ExceptionHelper.wrapOrThrow(ex)
            }
        }

    }
}